package com.mlog.njyjy.ndop.share.sql.dao;


import com.jfinal.plugin.activerecord.Page;
import com.mlog.njyjy.ndop.share.sql.support.SqlSort;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * .
 * </p>
 *
 * @author <a href="mailto:yingxiufeng@mlogcn.com">alex</a>
 * @version v1.0, 2019/7/4
 */
public interface SqlSimpleRepository<T, ID> {

    /**
     * count the table
     * @return
     */
    long count();

    /**
     * count with where clause
     * @param whereClause  eg `where a>? and b<=?`
     * @param para
     * @return
     */
    long count(String whereClause, Object... para);

    /**
     * find all
     *
     * @param sort {@link SqlSort}
     * @return
     */
    List<T> findAll(SqlSort sort);

    /**
     * find all without sort
     * @return
     */
    List<T> findAll();

    /**
     * find one by id
     *
     * @param id
     * @return
     */
    T findOne(ID id);

    /**
     * find first one
     *
     * @return
     */
    T findFirst(String sql);

    /**
     * find top n
     *
     * @param limit
     * @return
     */
    List<T> findTopN(int limit, String sql);

    /**
     * find one by some field
     *
     * @param fieldName
     * @param fieldValue
     * @return
     */
    List<T> findByField(String fieldName, Object fieldValue);

    /**
     * find by sql & para
     *
     * @param sql
     * @param para
     * @return
     */
    List<T> find(String sql, Object... para);

    /**
     * find by page
     * @param pageNumber 页码
     * @param pageSize  分页大小
     * @return
     */
    Page<T> findByPage(int pageNumber, int pageSize);

    /**
     * find by page with query sql
     * @param pageNumber 页码
     * @param pageSize   分页大小
     * @param sql        查询sql
     * @param paras      sql参数
     * @return
     */
    Page<T> findByPage(int pageNumber, int pageSize, String sql, Object... paras);

    /**
     * delete one by id
     *
     * @param id
     * @return
     */
    boolean delete(ID id);

    /**
     * insert
     *
     * @param entity
     * @return
     */
    boolean insert(T entity);

    /**
     * insert bulk
     *
     * @param entity
     * @return
     */
    boolean insert(List<T> entity);

    /**
     * update
     *
     * @param entity
     * @return
     */
    boolean update(T entity);

}
